package com.example.homework

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.homework.db.UserDBHelper
import com.example.homework.utils.Prefs

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val last = Prefs.lastAccount(this)
        if (last.isNullOrEmpty()) {
            // 首次/未注册
            startActivity(Intent(this, LoginActivity::class.java))
        } else {
            // 直接拿本地账号自动登录
            val db = UserDBHelper(this)
            val user = db.getUserByAccount(last)
            if (user == null) {   // 本地数据被清掉，重新注册
                startActivity(Intent(this, LoginActivity::class.java))
            } else {
                val intent = Intent(this, MainActivity::class.java).apply {
                    putExtra("user", user)   // 直接传递整个 User
                }
                startActivity(intent)
            }
        }
        finish()
    }
}